package com.xizi.lotterySystem.common.exception;

import com.xizi.lotterySystem.common.exception.errorCode.GlobalErrorCodeConstants;
import com.xizi.lotterySystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ServiceException.class)
    public CommonResult<Object> serviceException(ServiceException e) {
        log.error("serviceException异常：" ,e);
        return CommonResult.fail(e.getCode(), e.getMessage());
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ControllerException.class)
    public CommonResult<Object> controllerException(ControllerException e) {
        log.error("controllerException：" ,e);
        return CommonResult.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public CommonResult<Object> exception(Exception e) {
        log.error("服务异常：" ,e);
        return CommonResult.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
    }

}
